/*
 * PWM.cc
 *
 *  Created on: Mar 24, 2012
 *      Author: mscuser
 */
#include <string>
#include <fcntl.h>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <stdlib.h>
#include "FaultManager.h"
#include "ErrorManager.h"
#include "config.h"

#include "PWM.h"

PWM objDriverAtrialPacer("atrialpacer.csv", enAtrialActuator);
PWM objDriverVentricularPacer("ventricularpacer.csv", enVentricularActuator);
PWM objDriverReduntantAtrialPacer("atrialreduntantpacer.csv", enReduntantAtrialActuator);
PWM objDriverReduntantVentricularPacer("ventricularreduntantpacer.csv", enReduntantVentricularActuator);

int PWM::Read(int id){return 0;}

void PWM::Write(int amplitude, int frequency, int rate){
	std::string line;
	std::vector<int> v;
	//std::printf("PWM::Write\n");
	std::cout << "PWM::Write - ID " << driver_id << "amplitude " << amplitude << "frequency " << frequency << std::endl;
	
	char buffer[50];
	
	sprintf (buffer, "%d,%d,%d\n", amplitude, frequency, rate);
	/*
	if(pfilestream.is_open() ){
		std::cout << "pfilestream.is_open " <<std::endl;
		//pfilestream.write(buffer,13);
	}*/
	write(filedescr, buffer, std::strlen(buffer));
	
}
	


void PWM::Init(int id, int value){

}

PWM::PWM(){

}


PWM::PWM(char* filename, DEVICE_ID_TYPE id){
#ifdef PACE_DEBUG
   //filedescr = creat(filename, S_IWUSR);
	filedescr = open(filename, O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
#else
   filedescr = open(filename, O_WRONLY|O_APPEND|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
#endif

   driver_id = id;
}

PWM::~PWM(){
	/*
	if(pfilestream.is_open() ){
		pfilestream.close();
	}*/
	close(filedescr);
}
	
